<?php
  include('../config.php');
  header('Access-Control-Allow-Origin:*');
  $username = $_POST['username'];
  $password = $_POST['password'];
  // $age = $_POST['age'];

  //$sql = "insert into users (username,password) values ('$username','$password')";

  $sql2 = "select * from users where username='$username'";

  $res2 = mysql_query($sql2);
  
  $num = mysql_num_rows($res2);

  if ($num > 0) {
    echo json_encode(array(
      "code" => 202,
      "body" => array(
        "msg" => "用户名已存在"
      )
    ));
  }
  else {
    $sql = "insert into users (username,password) values ('$username','$password')";

    $res = mysql_query($sql);
    if ($res) {
      echo json_encode(array(
        "code" => 200,
        "body" => array(
          "msg" => "注册成功"
        )
      ));
    }

    else {
      echo json_encode(array(
        "code" => 201,
        "body" => array(
          "msg" => "注册失败，请重试"
        )
      ));
    }
  }
?>